from django.contrib.auth.models import AbstractUser
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models

# Create your models here.
from django.utils.safestring import mark_safe


# ----------------------用户权限关系-----------------------
class User(AbstractUser):
    """用户表"""
    genderChoices = ((0, '保密'), (1, '男'), (2, '女'))

    uid = models.BigAutoField(verbose_name="用户id", primary_key=True)
    username = models.CharField(verbose_name="用户名", max_length=64, unique=True)
    password = models.CharField(verbose_name='password', max_length=128,
                                help_text=mark_safe('''<a class='btn-link' href='password'>重置密码</a>'''))
    name = models.CharField(verbose_name="真实姓名", max_length=32)
    gender = models.SmallIntegerField(choices=genderChoices, default=0, verbose_name="性别")
    mobile = models.CharField(verbose_name="手机", max_length=11, unique=True, help_text="用于手机验证码登录")
    email = models.EmailField(verbose_name='邮箱', max_length=255, unique=True, blank=True, null=True)
    idCard = models.CharField(verbose_name="身份证号或护照号", db_column='id_card', max_length=32, blank=True, null=True)
    weChat = models.CharField(verbose_name="微信", db_column='we_chat', max_length=128, blank=True, null=True,
                              db_index=True)
    headImg = models.CharField(verbose_name="个人头像", db_column='head_img', max_length=256,
                               default='/static/frontend/head_portrait/logo@2x.png', blank=True, null=True)
    is_staff = models.BooleanField(verbose_name='员工状态', default=False, help_text='决定着用户是否可登录管理后台')
    date_joined = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)

    roles = models.ManyToManyField(verbose_name='用户所拥有的角色', to='Role')

    class Meta:
        db_table = 'user'
        verbose_name = '用户信息'
        verbose_name_plural = "用户信息"

    def __str__(self):
        return "%s(%s)" % (self.username, self.name)


class Role(models.Model):
    """角色表"""
    roleChoices = ((True, '超级用户'), (False, '普通用户'))

    rid = models.BigAutoField(verbose_name="角色id", primary_key=True)
    name = models.CharField(verbose_name='角色名称', max_length=32)
    isSuper = models.BooleanField(verbose_name='是否为超级用户', db_column='is_super', default=False, choices=roleChoices)
    permissions = models.ManyToManyField(to='Permission', verbose_name='角色所拥有的权限')

    class Meta:
        db_table = 'role'
        verbose_name = '角色信息'
        verbose_name_plural = "角色信息"

    def __str__(self):
        return "%s(%s)" % (self.name, self.get_isSuper_display())


class Menu(models.Model):
    """ 菜单表 """
    mid = models.BigAutoField(verbose_name="菜单id", primary_key=True)
    title = models.CharField(verbose_name='标题', max_length=32, unique=True)
    icon = models.CharField(max_length=32, verbose_name='图标', null=True, blank=True)
    weight = models.IntegerField(default=1, verbose_name='权重')

    class Meta:
        db_table = 'menu'
        verbose_name_plural = '菜单信息'
        verbose_name = '菜单信息'

    def __str__(self):
        return '%s(%s)' % (self.title, self.icon)


class Permission(models.Model):
    """ 权限表 """
    mid = models.BigAutoField(verbose_name="权限id", primary_key=True)
    title = models.CharField(verbose_name='标题', max_length=32, unique=True)
    url = models.CharField(verbose_name='权限', max_length=32, unique=True)
    name = models.CharField(verbose_name='URL别名', max_length=32, null=True, blank=True, unique=True)
    menu = models.ForeignKey(verbose_name='菜单', to='Menu', on_delete=models.CASCADE)
    parent = models.ForeignKey(verbose_name='父权限', to='Permission', on_delete=models.CASCADE)

    class Meta:
        db_table = 'permission'
        verbose_name_plural = '权限表'
        verbose_name = '权限表'

    def __str__(self):
        return self.title


# --------------------------------------------------------

# ----------------------停车场关系--------------------------
class Parking(models.Model):
    """停车场"""
    pid = models.BigAutoField(verbose_name="停车场id", primary_key=True)
    name = models.CharField(verbose_name='停车场名称', max_length=32)
    address = models.CharField(verbose_name='停车场地址', max_length=32, null=True, blank=True)
    appKey = models.CharField(verbose_name='停车场Key', db_column='app_key', max_length=64, blank=True)
    validTime = models.DateTimeField(verbose_name='停车场有效期', db_column='valid_time')
    latitude = models.DecimalField(verbose_name='纬度', max_digits=12, decimal_places=8, null=True, blank=True)
    longitude = models.DecimalField(verbose_name='经度', max_digits=12, decimal_places=8, null=True, blank=True)
    createTime = models.DateTimeField(verbose_name='停车场创建时间', db_column='create_time', auto_created=True)

    class Meta:
        db_table = 'parking'
        verbose_name_plural = '停车场'
        verbose_name = '停车场'

    def __str__(self):
        return self.name


class ParkArea(models.Model):
    """停车区域"""
    multiChoices = ((True, '开启'), (False, '关闭'))

    paid = models.BigAutoField(verbose_name="停车区域id", primary_key=True)
    name = models.CharField(verbose_name='停车区域名称', max_length=32)
    temporarySpace = models.IntegerField(verbose_name='临时车位', db_column='temporary_space', default=0)
    totalSpace = models.IntegerField(verbose_name='总车位', db_column='total_space', default=0)
    floor = models.IntegerField(verbose_name='区域楼层', default=1)
    multiSpace = models.BooleanField(verbose_name='是否开启多位多车', db_column='multi_space', choices=multiChoices,
                                     default=False)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'park_area'
        verbose_name_plural = '停车区域'
        verbose_name = '停车区域'

    def __str__(self):
        return '%s(%s)' % (self.name, self.parking.name)


class ParkDoor(models.Model):
    """停车出入口"""
    did = models.BigAutoField(verbose_name="停车出入口id", primary_key=True)
    name = models.CharField(verbose_name='停车出入口', max_length=32)
    latitude = models.DecimalField(verbose_name='纬度', max_digits=12, decimal_places=8, null=True, blank=True)
    longitude = models.DecimalField(verbose_name='经度', max_digits=12, decimal_places=8, null=True, blank=True)

    area = models.ForeignKey(verbose_name='所属停车区域', to='ParkArea', on_delete=models.CASCADE)

    class Meta:
        db_table = 'park_door'
        verbose_name_plural = '停车区域'
        verbose_name = '停车区域'

    def __str__(self):
        return '%s(%s)' % (self.name, self.area.name)


class Camera(models.Model):
    """设备表"""
    processTypeChoice = ((1, '入场'), (2, '出场'))

    cid = models.BigAutoField(verbose_name="设备id", primary_key=True)
    name = models.CharField(verbose_name='停车出入口', max_length=32)
    processType = models.IntegerField(verbose_name='出入类型', choices=processTypeChoice, db_column='process_type',
                                      default=1)
    cameraIp = models.CharField(verbose_name='设备Ip', max_length=32)

    door = models.ForeignKey(verbose_name='所属停车出入口', to='ParkDoor', on_delete=models.CASCADE)
    area = models.ForeignKey(verbose_name='所属停车区域', to='ParkArea', on_delete=models.CASCADE)

    class Meta:
        db_table = 'camera'
        verbose_name_plural = '设备'
        verbose_name = '设备'

    def __str__(self):
        return '%s(%s)' % (self.name, self.area.name)


# --------------------------------------------------------

# ----------------------车主关系---------------------------
class CarOwner(models.Model):
    """车主表"""
    genderChoices = ((0, '保密'), (1, '男'), (2, '女'))

    oid = models.BigAutoField(verbose_name="车主id", primary_key=True)
    name = models.CharField(verbose_name='停车场名称', max_length=32)
    gender = models.SmallIntegerField(verbose_name="性别", choices=genderChoices, default=0)
    mobile = models.CharField(verbose_name="手机", max_length=11, unique=True, help_text="用于手机验证码登录")
    balance = models.DecimalField(verbose_name='账户余额', max_digits=10, decimal_places=2, default=0.00)
    createTime = models.DateTimeField(verbose_name='车主创建时间', db_column='create_time', auto_created=True)

    user = models.ForeignKey(verbose_name='停车场用户', to='User', on_delete=models.CASCADE, null=True, blank=True)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'car_owner'
        verbose_name_plural = '车主'
        verbose_name = '车主'

    def __str__(self):
        return '%s(%s)' % (self.name, self.parking.name)


class Car(models.Model):
    """车辆表"""
    typeChoices = ((1, '小车'), (2, '大车'))

    cid = models.BigAutoField(verbose_name="车主id", primary_key=True)
    license = models.CharField(verbose_name='车辆表车牌', max_length=10)
    type = models.BooleanField(verbose_name='车辆类型', choices=typeChoices, default=1)
    brand = models.CharField(verbose_name='车辆品牌', max_length=10, null=True, blank=True)
    color = models.CharField(verbose_name='车辆颜色', max_length=10, null=True, blank=True)
    effectiveTime = models.DateTimeField(verbose_name="生效时间", db_column='effective_time', auto_now_add=True)
    validTime = models.DateTimeField(verbose_name="生效时间", db_column='valid_time', auto_now_add=True)

    owner = models.ForeignKey(verbose_name='所属车主', to='CarOwner', on_delete=models.CASCADE)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)
    group = models.ManyToManyField(to='CarAuthGroup', verbose_name='关联用户组')

    RenewalDetail = GenericRelation("RenewalDetail")

    class Meta:
        db_table = 'car'
        verbose_name_plural = '车辆'
        verbose_name = '车辆'

    def __str__(self):
        return '%s(%s,%s)' % (self.license, self.owner.name, self.parking.name)


class CarAuthGroup(models.Model):
    """用户组表"""
    typeChoices = ((1, '小车'), (2, '大车'))
    authTypeChoices = ((1, '临时车'), (2, '车辆会员'), (3, '车位会员'), (4, '储值会员'))
    invalidMemberTypeChoices = ((1, '转临时'), (2, '禁止通行'))

    aid = models.BigAutoField(verbose_name="用户组id", primary_key=True)
    name = models.CharField(verbose_name='用户组名称', max_length=32)
    type = models.BooleanField(verbose_name='车辆类型', choices=typeChoices, default=1)
    groupType = models.IntegerField(verbose_name='用户类型', db_column='group_type', choices=authTypeChoices, default=1)
    noRecordAuto = models.BooleanField(verbose_name='无入场记录出场自动放行', db_column='no_record_auto', default=False)
    isComputeSpace = models.BooleanField(verbose_name='是否参与剩余车位计算', db_column='is_compute_space', default=False)
    noChargeAuto = models.BooleanField(verbose_name='出场费用为0时自动放行', db_column='no_charge_auto', default=False)
    fullSpaceEnter = models.BooleanField(verbose_name='车位已满是否允许进入', db_column='full_space_enter', default=False)
    tmpEnterAuto = models.BooleanField(verbose_name='临停车入场是否自动放行', db_column='tmp_enter_auto', default=False)
    reserveNotEnoughEnter = models.BooleanField(verbose_name='储值车余额不足是否入场', db_column='reserve_not_enough_enter',
                                                default=False)
    invalidMemberType = models.BooleanField(verbose_name='会员过期通行方式', db_column='invalid_member_type',
                                            choices=invalidMemberTypeChoices, default=False)

    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)
    renewal = models.ForeignKey(verbose_name='续费模板', to='RenewalTemplate', on_delete=models.CASCADE, null=True,
                                blank=True)

    class Meta:
        db_table = 'car_auth_group'
        verbose_name_plural = '用户组'
        verbose_name = '用户组'

    def __str__(self):
        return '%s(%s,%s)' % (self.name, self.get_groupType_display(), self.parking.name)


class SpaceGroup(models.Model):
    """车位组表"""
    sid = models.BigAutoField(verbose_name="车位组id", primary_key=True)
    name = models.CharField(verbose_name='车位组名称', max_length=32)

    renewal = models.ForeignKey(verbose_name='续费模板', to='RenewalTemplate', on_delete=models.CASCADE, null=True,
                                blank=True)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'space_group'
        verbose_name_plural = '车位'
        verbose_name = '车位'

    def __str__(self):
        return '%s(%s)' % (self.name, self.parking.name)


class Space(models.Model):
    """车位表"""
    typeChoices = ((1, '普通车位'), (2, '机械车位'), (3, '充电车位'))

    sid = models.BigAutoField(verbose_name="车位id", primary_key=True)
    spaceNum = models.CharField(verbose_name='车位编号', db_column='space_num', max_length=32)
    type = models.BooleanField(verbose_name='车位类型', choices=typeChoices, default=1)
    effectiveTime = models.DateTimeField(verbose_name="生效时间", db_column='effective_time', auto_now_add=True)
    validTime = models.DateTimeField(verbose_name="生效时间", db_column='valid_time', auto_now_add=True)

    owner = models.ForeignKey(verbose_name='所属车主', to='CarOwner', on_delete=models.CASCADE, null=True, blank=True)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)
    group = models.ForeignKey(verbose_name='车位组', to='SpaceGroup', on_delete=models.CASCADE)

    RenewalDetail = GenericRelation("RenewalDetail")

    class Meta:
        db_table = 'space'
        verbose_name_plural = '车位'
        verbose_name = '车位'

    def __str__(self):
        return '%s(%s,%s)' % (self.spaceNum, self.get_type_display(), self.parking.name)


# --------------------------------------------------------

# ----------------------续费计费---------------------------
class RenewalTemplate(models.Model):
    """续费模板"""
    typeChoices = ((1, '延期模板'), (2, '储值模板'),)
    trimChoices = ((1, '自然日'), (2, '自然月'), (3, '30天'))
    storeChoices = ((1, '顺延'), (2, '跳延'))

    rid = models.BigAutoField(verbose_name="续费模板id", primary_key=True)
    name = models.CharField(verbose_name='续费模板名称', max_length=32)
    type = models.BooleanField(verbose_name='模板类型', choices=typeChoices, default=1)
    money = models.DecimalField(verbose_name='续费余额', max_digits=10, decimal_places=2, default=0.00)
    period = models.IntegerField(verbose_name='续费周期', default=1)
    trimType = models.IntegerField(verbose_name='计时方式', db_column='trim_type', choices=trimChoices, default=1)
    storeType = models.IntegerField(verbose_name='延期方式', db_column='store_type', choices=storeChoices, default=1)

    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'renewal_template'
        verbose_name_plural = '续费模板'
        verbose_name = '续费模板'

    def __str__(self):
        return '%s(%s,%s)' % (self.name, self.get_type_display(), self.parking.name)


class ChargingTemplate(models.Model):
    """计费模板"""
    typeChoices = ((1, '大车模板'), (2, '小车模板'),)

    cid = models.BigAutoField(verbose_name="计费模板id", primary_key=True)
    name = models.CharField(verbose_name='计费模板名称', max_length=32)
    type = models.BooleanField(verbose_name='模板类型', choices=typeChoices, default=1)
    freeTime = models.IntegerField(verbose_name='免费时长', db_column='free_time', default=0)
    chargingParam = models.TextField(verbose_name='模板参数', db_column='charging_param')

    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'charging_template'
        verbose_name_plural = '计费模板'
        verbose_name = '计费模板'

    def __str__(self):
        return '%s(%s,%s)' % (self.name, self.get_type_display(), self.parking.name)


class ChargingAreaAuth(models.Model):
    """区域-计费-用户组"""
    cid = models.BigAutoField(verbose_name="区域-计费-用户组id", primary_key=True)
    charging = models.ForeignKey(verbose_name='关联计费模板', to='ChargingTemplate', on_delete=models.CASCADE)
    area = models.ForeignKey(verbose_name='关联停车区域', to='ParkArea', on_delete=models.CASCADE)
    group = models.ForeignKey(verbose_name='关联用户组', to='CarAuthGroup', on_delete=models.CASCADE)

    class Meta:
        db_table = 'charging_area_auth'
        verbose_name_plural = '区域-计费-用户组'
        verbose_name = '区域-计费-用户组'

    def __str__(self):
        return '%s-%s-%s' % (self.area.name, self.charging.name, self.group.name)


# --------------------------------------------------------

# ----------------------账单管理---------------------------


class Bill(models.Model):
    """账单"""
    payMethodChoices = ((1, '微信'), (2, '支付宝'), (3, '现金'))
    payTypeChoices = ((1, '电子'), (2, '现金'))
    billTypeChoices = ((1, '临停'), (2, '车辆延期'), (1, '车位延期'), (2, '储值'))

    bid = models.BigAutoField(verbose_name="账单id", primary_key=True)

    billType = models.IntegerField(verbose_name='账单类型', choices=billTypeChoices, db_column='bill_method', default=1)

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    thirdOrderNum = models.CharField(verbose_name='第三方单号', max_length=64, db_column='third_order_num', null=True,
                                     blank=True)
    orderNum = models.CharField(verbose_name='订单单号', max_length=64, db_column='order_num', null=True,
                                blank=True)
    money = models.DecimalField(verbose_name='支付金额', max_digits=10, decimal_places=2, default=0.00)
    payTime = models.DateTimeField(verbose_name="支付时间", db_column='pay_time', auto_now_add=True)
    payMethod = models.IntegerField(verbose_name='支付方式', choices=payMethodChoices, db_column='pay_method', default=3)
    payType = models.IntegerField(verbose_name='支付类型', choices=payMethodChoices, db_column='pay_type', default=2)

    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'bill'
        verbose_name_plural = '账单'
        verbose_name = '账单'

    def __str__(self):
        return '%s(%s)' % (self.content_type.model, self.parking.name)


class ExitOrder(models.Model):
    """停车缴费记录"""
    payMethodChoices = ((1, '微信'), (2, '支付宝'), (3, '现金'), (4, '储值扣费'))
    payTypeChoices = ((1, '电子'), (2, '现金'), (3, '储值扣费'))

    eid = models.BigAutoField(verbose_name="停车缴费记录id", primary_key=True)
    parkingNum = models.CharField(verbose_name='停车单号', max_length=64, db_column='parking_num', null=True,
                                  blank=True)
    license = models.CharField(verbose_name='入场纠正车牌', max_length=10, null=True, blank=True)
    money = models.DecimalField(verbose_name='支付金额', max_digits=10, decimal_places=2, default=0.00)
    discount = models.DecimalField(verbose_name='支付金额', max_digits=10, decimal_places=2, default=0.00)
    payTime = models.DateTimeField(verbose_name="支付时间", db_column='pay_time', auto_now_add=True)
    payMethod = models.IntegerField(verbose_name='支付方式', choices=payMethodChoices, db_column='pay_method', default=3)
    payType = models.IntegerField(verbose_name='支付类型', choices=payMethodChoices, db_column='pay_type', default=2)

    employee = models.ForeignKey(verbose_name='收费员', to='User', on_delete=models.CASCADE)
    bill = GenericRelation("Bill")

    class Meta:
        db_table = 'exit_order'
        verbose_name_plural = '停车缴费记录'
        verbose_name = '停车缴费记录'

    def __str__(self):
        return '%s(%s)' % (self.license, self.bill.parking.name)


class RenewalDetail(models.Model):
    """续费明细"""
    payMethodChoices = ((1, '微信'), (2, '支付宝'), (3, '现金'))
    payTypeChoices = ((1, '电子'), (2, '现金'))
    objectTypeChoices = ((1, '车辆'), (2, '车位'))
    renewalTypeChoices = ((1, '模版续费'), (2, '自定义续费'))
    trimChoices = ((1, '自然日'), (2, '自然月'), (3, '30天'))

    rid = models.BigAutoField(verbose_name="续费明细id", primary_key=True)
    objectType = models.IntegerField(verbose_name='账单类型', choices=objectTypeChoices, db_column='bill_method', default=1)

    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    effectiveTime = models.DateTimeField(verbose_name="开始时间", db_column='effective_time', null=True, blank=True)
    validTime = models.DateTimeField(verbose_name="结束时间", db_column='valid_time', null=True, blank=True)
    beforeDescription = models.CharField(verbose_name='续费前有效期（起始-结束）', db_column='before_description', max_length=128,
                                         null=True, blank=True)
    renewalType = models.IntegerField(verbose_name='续费类型', choices=renewalTypeChoices, db_column='renewal_type',
                                      default=1)
    renewalName = models.CharField(verbose_name='续费模版名称', db_column='renewal_name', max_length=128,
                                   null=True, blank=True)
    renewalPeriod = models.IntegerField(verbose_name='续费周期', db_column='renewal_period', default=1)
    renewalTrim = models.IntegerField(verbose_name='计时方式', db_column='renewal_trim', choices=trimChoices, default=1)

    money = models.DecimalField(verbose_name='支付金额', max_digits=10, decimal_places=2, default=0.00)
    payTime = models.DateTimeField(verbose_name="支付时间", db_column='pay_time', auto_now_add=True)
    payMethod = models.IntegerField(verbose_name='支付方式', choices=payMethodChoices, db_column='pay_method', default=3)
    payType = models.IntegerField(verbose_name='支付类型', choices=payMethodChoices, db_column='pay_type', default=2)

    employee = models.ForeignKey(verbose_name='收费员', to='User', on_delete=models.CASCADE)
    bill = GenericRelation("Bill")

    class Meta:
        db_table = 'renewal_detail'
        verbose_name_plural = '停车缴费记录'
        verbose_name = '停车缴费记录'

    def __str__(self):
        return '%s(%s)' % (self.get_objectType_display(), self.bill.parking.name)


class ReserveRecord(models.Model):
    """储值纪录"""
    payMethodChoices = ((1, '微信'), (2, '支付宝'), (3, '现金'))
    payTypeChoices = ((1, '电子'), (2, '现金'))
    operTypeChoices = ((1, '储值'), (2, '充值'))

    rid = models.BigAutoField(verbose_name="续费明细id", primary_key=True)

    money = models.DecimalField(verbose_name='支付金额', max_digits=10, decimal_places=2, default=0.00)
    payTime = models.DateTimeField(verbose_name="支付时间", db_column='pay_time', auto_now_add=True)
    payMethod = models.IntegerField(verbose_name='支付方式', choices=payMethodChoices, db_column='pay_method', default=3)
    payType = models.IntegerField(verbose_name='支付类型', choices=payMethodChoices, db_column='pay_type', default=2)
    operType = models.IntegerField(verbose_name='操作类型', choices=operTypeChoices, db_column='oper_type', default=1)
    reserveMoney = models.DecimalField(verbose_name='余额', db_column='reserve_money', max_digits=10, decimal_places=2,
                                       default=0.00)

    owner = models.ForeignKey(verbose_name='车主', to='CarOwner', on_delete=models.CASCADE)
    employee = models.ForeignKey(verbose_name='收费员', to='User', on_delete=models.CASCADE)
    bill = GenericRelation("Bill")

    class Meta:
        db_table = 'reserve_record'
        verbose_name_plural = '停车缴费记录'
        verbose_name = '停车缴费记录'

    def __str__(self):
        return '%s(%s)' % (self.owner.name, self.bill.parking.name)


class ReserveOrder(models.Model):
    """储值消费"""
    reserveTypeChoices = ((1, '临停消费'), (2, '其他'))

    rid = models.BigAutoField(verbose_name="续费明细id", primary_key=True)

    money = models.DecimalField(verbose_name='支付金额', max_digits=10, decimal_places=2, default=0.00)
    payTime = models.DateTimeField(verbose_name="支付时间", db_column='pay_time', auto_now_add=True)
    reserveType = models.IntegerField(verbose_name='支付类型', choices=reserveTypeChoices, db_column='reserve_type',
                                      default=2)
    reserveMoney = models.DecimalField(verbose_name='余额', db_column='reserve_money', max_digits=10, decimal_places=2,
                                       default=0.00)

    owner = models.ForeignKey(verbose_name='车主', to='CarOwner', on_delete=models.CASCADE)
    order = models.ForeignKey(verbose_name='缴费记录', to='ExitOrder', on_delete=models.CASCADE)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'reserve_order'
        verbose_name_plural = '储值消费'
        verbose_name = '储值消费'

    def __str__(self):
        return '%s(%s)' % (self.owner.name, self.parking.name)


# --------------------------------------------------------

# ----------------------过车管理---------------------------

class PassRecord(models.Model):
    """通行记录"""
    licenseTypeChoices = ((0, '未知'),
                          (1, '蓝牌'),
                          (2, '单层黄牌车'),
                          (3, '警车'),
                          (4, '武警车辆'),
                          (5, '双层黄牌'),
                          (6, '摩托车'),
                          (7, '教练车牌'),
                          (8, '军车'),
                          (9, '个性化车牌'),
                          (10, '港澳车牌'),
                          (12, '老式车牌（不反光）'))

    rid = models.BigAutoField(verbose_name="通行记录id", primary_key=True)
    license = models.CharField(verbose_name='扫描车牌', max_length=10)
    scanTime = models.DateTimeField(verbose_name="扫描时间", db_column='scan_time', null=True, blank=True)
    scanImg = models.CharField(verbose_name='扫描图片', db_column='scan_img', max_length=128, null=True, blank=True)
    licenseType = models.IntegerField(verbose_name='车牌类型', choices=licenseTypeChoices, db_column='license_type',
                                      default=1)
    licenseColor = models.CharField(verbose_name='车牌颜色', db_column='license_color', max_length=10, null=True,
                                    blank=True)
    carColor = models.CharField(verbose_name='车辆颜色', db_column='car_color', max_length=10, null=True, blank=True)
    accuracy = models.DecimalField(verbose_name='识别准确度', max_digits=8, decimal_places=2, default=0.00)

    camera = models.ForeignKey(verbose_name='扫描设备', to='Camera', on_delete=models.CASCADE)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'pass_record'
        verbose_name_plural = '通行记录'
        verbose_name = '通行记录'

    def __str__(self):
        return '%s(%s)' % (self.license, self.parking.name)


class EnterRecord(models.Model):
    """入场记录"""
    licenseTypeChoices = ((0, '未知'),
                          (1, '蓝牌'),
                          (2, '单层黄牌车'),
                          (3, '警车'),
                          (4, '武警车辆'),
                          (5, '双层黄牌'),
                          (6, '摩托车'),
                          (7, '教练车牌'),
                          (8, '军车'),
                          (9, '个性化车牌'),
                          (10, '港澳车牌'),
                          (12, '老式车牌（不反光）'))

    eid = models.BigAutoField(verbose_name="入场记录id", primary_key=True)
    parkingNum = models.CharField(verbose_name='停车单号', max_length=64, db_column='parking_num', null=True,
                                  blank=True)
    license = models.CharField(verbose_name='扫描车牌', max_length=10)
    correctLicense = models.CharField(verbose_name='纠正车牌', max_length=10, db_column='correct_license')
    card = models.CharField(verbose_name='卡号', max_length=32, null=True, blank=True)
    enterTime = models.DateTimeField(verbose_name="入场时间", db_column='enter_time', auto_now_add=True)
    scanImg = models.CharField(verbose_name='扫描图片', db_column='scan_img', max_length=128, null=True, blank=True)
    licenseImg = models.CharField(verbose_name='车牌图', db_column='license_img', max_length=128, null=True, blank=True)
    licenseType = models.IntegerField(verbose_name='车牌类型', choices=licenseTypeChoices, db_column='license_type',
                                      default=1)

    camera = models.ForeignKey(verbose_name='入场设备', to='Camera', on_delete=models.CASCADE)
    group = models.ForeignKey(verbose_name='用户组', to='CarAuthGroup', on_delete=models.CASCADE)
    area = models.ForeignKey(verbose_name='停车区域', to='ParkArea', on_delete=models.CASCADE)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'enter_record'
        verbose_name_plural = '入场记录'
        verbose_name = '入场记录'

    def __str__(self):
        return '%s(%s)' % (self.license, self.parking.name)


class SiteVehicle(models.Model):
    """场内车辆"""
    licenseTypeChoices = ((0, '未知'),
                          (1, '蓝牌'),
                          (2, '单层黄牌车'),
                          (3, '警车'),
                          (4, '武警车辆'),
                          (5, '双层黄牌'),
                          (6, '摩托车'),
                          (7, '教练车牌'),
                          (8, '军车'),
                          (9, '个性化车牌'),
                          (10, '港澳车牌'),
                          (12, '老式车牌（不反光）'))

    sid = models.BigAutoField(verbose_name="场内车辆id", primary_key=True)
    parkingNum = models.CharField(verbose_name='停车单号', max_length=64, db_column='parking_num', null=True,
                                  blank=True)
    license = models.CharField(verbose_name='扫描车牌', max_length=10)
    correctLicense = models.CharField(verbose_name='纠正车牌', max_length=10, db_column='correct_license')
    card = models.CharField(verbose_name='卡号', max_length=32, null=True, blank=True)
    enterTime = models.DateTimeField(verbose_name="入场时间", db_column='enter_time', auto_now_add=True)
    scanImg = models.CharField(verbose_name='扫描图片', db_column='scan_img', max_length=128, null=True, blank=True)
    licenseImg = models.CharField(verbose_name='车牌图', db_column='license_img', max_length=128, null=True, blank=True)
    licenseType = models.IntegerField(verbose_name='车牌类型', choices=licenseTypeChoices, db_column='license_type',
                                      default=1)

    camera = models.ForeignKey(verbose_name='相机', to='Camera', on_delete=models.CASCADE)
    group = models.ForeignKey(verbose_name='用户组', to='CarAuthGroup', on_delete=models.CASCADE)
    area = models.ForeignKey(verbose_name='停车区域', to='ParkArea', on_delete=models.CASCADE)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'site_vehicle'
        verbose_name_plural = '场内车辆'
        verbose_name = '场内车辆'

    def __str__(self):
        return '%s(%s)' % (self.license, self.parking.name)


class ExitRecord(models.Model):
    """车辆出场记录"""
    licenseTypeChoices = ((0, '未知'),
                          (1, '蓝牌'),
                          (2, '单层黄牌车'),
                          (3, '警车'),
                          (4, '武警车辆'),
                          (5, '双层黄牌'),
                          (6, '摩托车'),
                          (7, '教练车牌'),
                          (8, '军车'),
                          (9, '个性化车牌'),
                          (10, '港澳车牌'),
                          (12, '老式车牌（不反光）'))
    clientTypeChoices = ((1, 'web'), (2, 'pc'), (3, 'mobile'))

    eid = models.BigAutoField(verbose_name="车辆出场记录id", primary_key=True)
    parkingNum = models.CharField(verbose_name='停车单号', max_length=64, db_column='parking_num', null=True,
                                  blank=True)
    enterLicense = models.CharField(verbose_name='入场车牌', max_length=10, db_column='enter_license')
    enterTime = models.DateTimeField(verbose_name="入场时间", db_column='enter_time', auto_now_add=True)
    enterImg = models.CharField(verbose_name='入场图片', db_column='enter_img', max_length=128, null=True, blank=True)
    enterLicenseImg = models.CharField(verbose_name='入场车牌图', db_column='enter_license_img', max_length=128, null=True,
                                       blank=True)

    exitLicense = models.CharField(verbose_name='出场车牌', max_length=10, db_column='exit_license')
    exitTime = models.DateTimeField(verbose_name="出场时间", db_column='exit_time', auto_now_add=True)
    exitImg = models.CharField(verbose_name='出场图片', db_column='exit_img', max_length=128, null=True, blank=True)
    exitLicenseImg = models.CharField(verbose_name='出场车牌图', db_column='exit_license_img', max_length=128, null=True,
                                      blank=True)

    licenseType = models.IntegerField(verbose_name='车牌类型', choices=licenseTypeChoices, db_column='license_type',
                                      default=1)
    isAbnormal = models.BooleanField(verbose_name='是否异常放行', default=False, db_column='is_abnormal')
    abnormalReason = models.CharField(verbose_name='异常放行原因', db_column='abnormal_reason', max_length=128, null=True,
                                      blank=True)
    clientType = models.IntegerField(verbose_name='客户端类型', choices=clientTypeChoices, db_column='client_type',
                                     default=1)
    money = models.DecimalField(verbose_name='本次应缴费用', max_digits=10, decimal_places=2, default=0.00)

    enter_camera = models.ForeignKey(verbose_name='入场设备', to='Camera', related_name='enterCamera',
                                     on_delete=models.CASCADE)
    exit_camera = models.ForeignKey(verbose_name='出场设备', to='Camera', related_name='exitCamera',
                                    on_delete=models.CASCADE)
    group = models.ForeignKey(verbose_name='用户组', to='CarAuthGroup', on_delete=models.CASCADE)
    parking = models.ForeignKey(verbose_name='所属停车场', to='Parking', on_delete=models.CASCADE)

    class Meta:
        db_table = 'exit_record'
        verbose_name_plural = '车辆出场记录'
        verbose_name = '车辆出场记录'

    def __str__(self):
        return '%s(%s)' % (self.parkingNum, self.parking.name)
# --------------------------------------------------------
